usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespace ObserverExample
{

	classProgram
	{

		static void Main(string[] args)
		{

			ButtonPager buttonPager = new ButtonPager();

			ListPager listPager = new ListPager();

			Controlgridview = new GridView();

			Controllabel = new Label();

			buttonPager.changePageHandler += newPager.ChangePageHandler(buttonPager.ChangePage);

			buttonPager.changePageHandler += newPager.ChangePageHandler(gridview.ChangePage);

			buttonPager.changePageHandler += newPager.ChangePageHandler(label.ChangePage);

			buttonPager.changePageHandler += newPager.ChangePageHandler(listPager.ChangePage);

			listPager.changePageHandler += newPager.ChangePageHandler(buttonPager.ChangePage);

			listPager.changePageHandler += newPager.ChangePageHandler(gridview.ChangePage);

			listPager.changePageHandler += newPager.ChangePageHandler(label.ChangePage);

			listPager.changePageHandler += newPager.ChangePageHandler(listPager.ChangePage);

			buttonPager.NextPage();

			Console.WriteLine();

			buttonPager.NextPage();

			Console.WriteLine();

			buttonPager.NextPage();

			Console.WriteLine();

			buttonPager.PreviousPage();

			Console.WriteLine();

			buttonPager.PreviousPage();

			Console.WriteLine();

			listPager.SelectPage(2);

			Console.WriteLine();

			listPager.SelectPage(1);

			Console.WriteLine();

			listPager.SelectPage(0);

		}

	}

	abstract class Pager
	{

		protected intpageIndex = 0;

		public int PageIndex
		{

			get{ returnpageIndex; }

			set{ pageIndex = value; }

		}

		protected int pageCount = 3;

		public int PageCount
		{

			get{ returnpageCount; }

		}

		public event ChangePageHandler changePageHandler;

		public delegate void ChangePageHandler(Pagersender);

		protected void ChangePage()
		{

			if(changePageHandler != null) {
				changePageHandler(this);
			}

		}

	}

	class ButtonPager: Pager, Control
	{

		public void NextPage()
		{

			if(pageIndex < pageCount - 1) {

				Console.WriteLine("Click NextPage Button...");

				pageIndex++;

				ChangePage();

			}

		}

		public void PreviousPage()
		{

			if(pageIndex > 0) {

				Console.WriteLine("Click PreviousPage Button...");

				pageIndex--;

				ChangePage();

			}

		}

		public void ChangePage(Pagersender)
		{

			base.pageIndex = sender.PageIndex;

			if(pageIndex > 0 && pageIndex < pageCount - 1)

				Console.WriteLine("< >" );

			elseif(pageIndex == 0)

				Console.WriteLine("Next>>");

			else

				Console.WriteLine("<);

		}

	}

	class ListPager: Pager, Control
	{

		public void SelectPage(intpageIndex)

		{

			if(pageIndex >= 0 && pageIndex < pageCount) {

				Console.WriteLine(string.Format("Click <{0}> Link...", pageIndex + 1));

				base.pageIndex = pageIndex;

				ChangePage();

			}

		}

		public void ChangePage(Pagersender) {

		base.pageIndex = sender.PageIndex;

		for(inti = 1; i <= pageCount; i++) {

			if(pageIndex + 1 == i)

				Console.Write(string.Format(" <{0}> ", i));

			else

				Console.Write(string.Format(" {0} ", i));

		}

			Console.WriteLine();

		}

	} //end class

	interface Control
	{

		void ChangePage(Pagersender);

	}

	class	GridView: Control
	{

		public void ChangePage(Pager sender)
		{

			Console.WriteLine(string.Format("GridView->Show data of page {0}", sender.PageIndex + 1));

		}

	}

	class Label: Control
	{

		public void ChangePage(Pager sender)
		{

			Console.WriteLine(string.Format("Label.Text=[{0}/{1}]", sender.PageIndex + 1, sender.PageCount));

		}

	}

}